package cn.bytecollege.admin.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Admin;
import cn.bytecollege.common.util.DBHelper;

import java.util.List;

public class AdminDao extends BaseDao {
    private DBHelper helper = null;

    public AdminDao() {
        helper = new DBHelper();
    }

    /**
     * 根据管理员账号查询管理员信息
     * @author zsm
     * @param adminUsername
     * @return
     */
    public Admin findAdminByAdminId(String adminUsername){
        String sql = "SELECT ADMIN_ID,ADMIN_USERNAME,ADMIN_PASSWORD,ADMIN_NAME,TURNOVER " +
                "FROM ADMIN WHERE ADMIN_USERNAME = ? ";
        List<Admin> list = helper.executeQuery(sql,Admin.class,adminUsername);
        Admin admin = null;
        if (list.size()>0){
            admin = list.get(0);
        }
        return admin;
    }

    /**
     * 保存管理员信息
     * @author zsm
     * @param admin
     * @return
     */
    public int saveAdmin(Admin admin){
        String sql = "INSERT INTO ADMIN (ADMIN_USERNAME,ADMIN_NAME,TURNOVER,ADMIN_PASSWORD) VALUES (?,?,?,?)";
        Object[] objects = {admin.getAdminUsername(),admin.getAdminName(),admin.getTurnover(),
                admin.getAdminPassword()};
        return helper.executeUpdate(sql,objects);
    }

    /**
     * 修改管理员的营业额
     * @param turnover
     * @param adminId
     * @return
     */
    public  int saveTurnover(Double turnover,Integer adminId){
        String sql = "UPDATE ADMIN SET TURNOVER=? WHERE ADMIN_ID=? ";
        Object[] objects = {turnover,adminId};
        return helper.executeUpdate(sql,objects);
    }

}
